package cn.signalpha.modules.shop.service;

import cn.signalpha.modules.shop.entity.GoodsSku;
import cn.signalpha.modules.shop.mapper.GoodsSkuMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import static cn.signalpha.common.cache.CacheNames.EXPIRATION_TIME_7;
import static cn.signalpha.common.cache.CacheNames.GOODS_SKU;

/**
 * 商品sku 服务类
 *
 * @author suguangjun
 */
@Service
public class GoodsSkuService extends ServiceImpl<GoodsSkuMapper, GoodsSku> {

	@Cacheable(cacheNames = GOODS_SKU + EXPIRATION_TIME_7, key = "#goodsId")
    public List<GoodsSku> listByGoodsId(Long goodsId) {
        return list(Wrappers.<GoodsSku>lambdaQuery().eq(GoodsSku::getGoodsId, goodsId));
    }

	@CacheEvict(cacheNames = GOODS_SKU, key = "#goodsId")
    public void removeByGoodsId(Long goodsId) {
        baseMapper.delete(Wrappers.<GoodsSku>lambdaQuery().eq(GoodsSku::getGoodsId, goodsId));
    }

	@CacheEvict(cacheNames = GOODS_SKU, key = "#goodsId")
	public void removeCacheByGoodsId(Long goodsId) {
	}

	/**
	 * 减少库存
	 */
	public int subtractStocks(Long skuId, Integer stock) {
		return baseMapper.subtractStocks(skuId, stock);
	}

	/**
	 * 返回库存
	 */
	public int returnStock(Map<Long, Integer> goodsSkuCollect) {
		return baseMapper.returnStock(goodsSkuCollect);
	}
}
